package com.ita.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ita.dao.CustomerDao;
import com.ita.pojo.Customer;

@Repository(value = "customerDao")
public class CustomerDaoImpl implements CustomerDao {

	@Autowired
	private EntityManagerFactory factory;

	@Override
	public Customer findUser(String uname, String password) {
		EntityManager manager = null;
		EntityTransaction tx = null;
		Customer c = null;
		try {
			manager = factory.createEntityManager();
			tx = manager.getTransaction();
			Query query = manager.createQuery("select c from Customer c where c.uname=:uname and c.password=:password");
			query.setParameter("uname", uname);
			query.setParameter("password", password);
			tx.begin();
			c = (Customer) query.getSingleResult();
			tx.commit();
			return c;
		} catch (Exception e) {
			tx.rollback();
			return c;
		} finally {
			manager.close();
		}

	}

	@Override
	public int addUser(Customer u) {
		EntityManager manager = null;
		EntityTransaction tx = null;
		try {
			manager = factory.createEntityManager();
			tx = manager.getTransaction();
			tx.begin();
			manager.persist(u);
			tx.commit();
			return 1;
		} catch (Exception e) {
			tx.rollback();
			return 0;
		} finally {
			manager.close();
		}

	}

	@Override
	public int update(Customer c) {
		EntityManager manager = null;
		EntityTransaction tx = null;
		int result=0;
		try {
			manager = factory.createEntityManager();
			tx = manager.getTransaction();
			tx.begin();
			Query query = manager.createQuery("update Customer c set c.uname=:uname, c.password=:password, c.age=:age where c.id=:id");
			query.setParameter("uname", c.getUname());
			query.setParameter("password", c.getPassword());
			query.setParameter("age", c.getAge());
			query.setParameter("id", c.getId());
			result=query.executeUpdate();
			tx.commit();
			return result;
		} catch (Exception e) {
			tx.rollback();
			return result;
		} finally {
			manager.close();
		}
	}

	@Override
	public int delete(String id) {
		EntityManager manager = null;
		EntityTransaction tx = null;
		try {
			manager = factory.createEntityManager();
			tx = manager.getTransaction();
			tx.begin();
			Customer c = manager.find(Customer.class, id);
			manager.remove(c);
			manager.flush();
			tx.commit();
			return 1;
		} catch (Exception e) {
			tx.rollback();
			return 0;
		} finally {
			manager.close();
		}
	}

	@Override
	public List<Customer> findAll() {
		EntityManager manager = null;
		EntityTransaction tx = null;
		List<Customer> list = null;
		try {
			manager = factory.createEntityManager();
			tx = manager.getTransaction();
			tx.begin();
			Query query = manager.createQuery("from Customer c");
			list = query.getResultList();
			tx.commit();
			return list;
		} catch (Exception e) {
			tx.rollback();
			return list;
		} finally {
			manager.close();
		}
	}
}
